package com.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.db.DbConnection;

public class Login {
	public Map<String, Object> doLogin(String username, String pass) {

		String sql = "select * from user where username='" + username
				+ "' and password='" + pass + "'";
		Connection con = null;

		Map<String, Object> user = null;
		try {
			con = DbConnection.getInstance().getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);

			if (rs.next()) {
				user = new HashMap<String, Object>();
				user.put("user_id", rs.getString("id"));
				user.put("user_name", rs.getString("username"));
				user.put("user_password", rs.getString("password"));
				user.put("user_city", rs.getString("city"));
				user.put("user_sex", rs.getString("sex"));
				user.put("user_age", rs.getString("age"));
				user.put("user_job", rs.getString("job"));
				user.put("user_view", rs.getString("ifview"));
				user.put("user_email", rs.getString("email"));
				user.put("createtime", rs.getDate("createtime"));

			}
		} catch (SQLException e) {
			user = null;
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	public void changeInfo(String formerUsername, String username, String sex,
			String age, String city, String job, String email, String ifview) {

		String sql = "update user set username='" + username + "' ,sex='" + sex
				+ "' ,age='" + age + "' ,job='" + job + "' ,city='" + city
				+ "' ,email='" + email + "' ,ifview='" + ifview
				+ "' where username='" + formerUsername + "'";

		Connection con = null;

		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();

			st.executeUpdate(sql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public String changePwd(String username, String password) {
		String sql = "update user set password='" + password + "' where username='"
				+ username + "'";

		
		Connection con = null;
		int result = 0;
		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();
			
			result = st.executeUpdate(sql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(result>0){//成功
			return "succeed";
		}else
			return "fail";
		
	}
}